﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using BGSpaceShooter;

namespace BGSpaceShooterServer
{
    internal class PlayerController
    {
        private static TimeSpan timeout = TimeSpan.FromSeconds(10.0);

        public PlayerManager PlayerManager { get; set; }


        public PlayerController(int maxPlayers)
        {
            PlayerManager = new PlayerManager(maxPlayers);
        }

        public int RegisterNewPlayer(ClientToServerPacket packet, IPEndPoint ipEndPoint)
        {
            return PlayerManager.AddPlayer(PlayerManager.CreatePlayer(packet, ipEndPoint));
        }

        public void RemovePlayer(int playerId)
        {
            PlayerManager.RemovePlayer(playerId);
        }

        public IEnumerable<Player> GetActivePlayers()
        {
            return PlayerManager.GetListOfActivePlayers();
        }

        public Player GetPlayerById(int playerId)
        {
            return PlayerManager.GetPlayerByID(playerId);
        }

        public List<ServerStatusResponse> GetUpdates()
        {
            List<ServerStatusResponse> statusResponses = new List<ServerStatusResponse>();
            foreach (Player player in GetActivePlayers())
            {
                statusResponses.Add(player.GetStatusResponse());
            }

            return statusResponses;
        }

        public List<ServerStatusResponse> GetHPUpdates()
        {
            List<ServerStatusResponse> statusResponses = new List<ServerStatusResponse>();
            foreach (Player player in GetActivePlayers())
            {
                statusResponses.AddRange(player.GetHPUpdates());
            }

            return statusResponses;
        }

        public IEnumerable<ServerStatusResponse> CheckIfPlayersAreDead()
        {
            var result = new List<ServerStatusResponse>();
            foreach (var player in PlayerManager.GetListOfActivePlayers())
            {
                if (player.StarFighter.HealthPercent < 0.01f)
                {
                    if (player.StarFighter.LastPlayerIdHit >= 0)
                    {
                        PlayerManager.GetPlayerByID(player.StarFighter.LastPlayerIdHit).Points++;
                        result.Add(new ServerStatusResponse()
                                       {
                                           Message = "Gracz " +
                                               player.StarFighter.LastPlayerIdHit + ": " +
                                               PlayerManager.GetPlayerByID(player.StarFighter.LastPlayerIdHit).Points.
                                                   ToString()
                                       });
                    }
                    player.Reset();
                }
            }
            return result;
        }

        public IEnumerable<ServerStatusResponse> KickRetiredPlayers()
        {
            List<ServerStatusResponse> responses = new List<ServerStatusResponse>();
            DateTime now = DateTime.Now;
            foreach (Player activePlayer in PlayerManager.GetActivePlayers())
            {
                if (now - activePlayer.LastRequest > timeout)
                {
                    PlayerManager.RemovePlayer(activePlayer.PlayerID);
                    responses.Add(new ServerStatusResponse()
                                      {
                                          GameObjectName = GameObjectName.Viper,
                                          gameObjectType = GameObjectType.StarFighter,
                                          playerId = activePlayer.PlayerID
                                      });
                }
            }
            return responses;
        }
    }
}
